package org.darkforest.ucenter.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.darkforest.dao.mapper.RefundReasonMapper;
import org.darkforest.enums.OrderStatusEnum;
import org.darkforest.enums.SellerStateEnum;
import org.darkforest.dao.mapper.OrderMapper;
import org.darkforest.dao.mapper.RefundReturnMapper;
import org.darkforest.model.*;
import org.darkforest.ucenter.service.ReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * 项目名称         :bwmall-ucenter
 * 文件名           :ReturnServiceImpl.java
 * 开发公司及作者    :陈超
 * 版本号及开发日期  :Rev. 1.00 (2015年2月20日)
 */
@Service
public class ReturnServiceImpl implements ReturnService {
	
	@Autowired
	private RefundReturnMapper refundReturnMapper;
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private RefundReasonMapper refundReasonMapper;

	@Override
	public void getMoneyList(RefundReturnSearchBean page) {
		page.setResultList(refundReturnMapper.selectByMember(page));
	}

	@Override
	public RefundReturnVo getMoneyDetail(Integer refundId, Integer memberId) {
		Map<String, Object> p = new HashMap<String, Object>();
		p.put("refundId", refundId);
		p.put("memberId", memberId);
		return refundReturnMapper.selectById(p);
	}

	@Override
	public RefundReturn addReturn(RefundReturnVo reVo) {
		Map<String, Integer> param = new HashMap<String, Integer>();
		param.put("id", reVo.getOrderId());
		param.put("buyerId", reVo.getBuyerId());
		OrderVo vo = orderMapper.selectById(param);
		OrderGoods goods = null;
		RefundReturn r = new RefundReturn();
		for(OrderGoods g : vo.getGoods()){
			if(g.getGoodsId().equals(reVo.getGoodsId())){
				goods = g;
				break;
			}
		}
		if(vo != null && vo.getOrderState().equals(OrderStatusEnum.COMPLETE.getValue()+"") && vo.getLockState() == 0){
			Order o = new Order();
			o.setOrderId(vo.getOrderId());
			o.setLockState(1);
			orderMapper.updateByPrimaryKeySelective(o);

			r.init();
			r.setOrderId(vo.getOrderId());
			r.setOrderSn(vo.getOrderSn() + "");
			r.setStoreId(vo.getStoreId());
			r.setStoreName(vo.getStoreName());
			r.setBuyerId(vo.getBuyerId());
			r.setBuyerName(vo.getBuyerName());
			r.setRefundAmount(vo.getOrderAmount().doubleValue() < reVo.getRefundAmount().doubleValue() ? vo.getOrderAmount() : reVo.getRefundAmount());
			r.setSellerState(SellerStateEnum.PROCESSED.getValue());
			r.setReasonInfo(reVo.getReasonInfo());
			r.setGoodsId(goods.getGoodsId());
			r.setGoodsImage(goods.getGoodsImage());
			r.setGoodsName(goods.getGoodsName());
			r.setGoodsState(1);
			r.setReturnType(2);
			if(reVo.getGoodsNum() == null){
				r.setRefundType(1);
			}else{
				r.setGoodsNum(goods.getGoodsNum() < reVo.getGoodsNum() ? goods.getGoodsNum() : reVo.getGoodsNum());
				r.setRefundType(2);
			}
			r.setReasonId(reVo.getReasonId());
			RefundReason refundReason = refundReasonMapper.selectByPrimaryKey(reVo.getReasonId());
			if(refundReason != null){
				r.setReasonInfo(refundReason.getReasonInfo());
			}
			r.setBuyerMessage(reVo.getBuyerMessage());
			r.setPicInfo(reVo.getPicInfo());
			r.setReturnChannel(vo.getPaymentCode());
			r.setOrderAmount(vo.getOrderAmount());
			refundReturnMapper.insertSelective(r);
		}
		return r;
	}

}
